<?php

namespace App\Abstracts\Models;


use Hilaoyu\LaravelExtend\Model;

abstract class AbstractModelCatalogs extends Model
{

    protected $table = 'catalogs';

    protected $fillable = [
        'name',
        'icon',
        'summary',
        'parent_id',
        'group',
        'status',
    ];

    protected $casts = [
        "status" => 'string'
    ];

    protected $appends = [
        'group_name',
        'status_name',
    ];


    public function getAllowedGroups(){
        return [

        ];

    }
    public function getAllowedGroupName($key=''){
        $key = trim($key);
        $groups = $this->getAllowedGroups();
        return $groups[$key] ?? '';
    }


    public function getValidationRules()
    {
        $rules = [
            'name' => 'required|string|max:60|unique:'. static::getTableCon() .',name,' . $this->getOriginalKey() . ',' . static::getPkName().',group,'.$this->getAttribute('group'),
            'parent_id' => 'nullable|uuid',
            'group' => 'required|in:' . implode(',',array_keys($this->getAllowedGroups()))
        ];

        return $rules;
    }

    public function getGroupNameAttribute()
    {
        $group = $this->getAttribute('group');

        return $group ? $this->getAllowedGroupName($group) : '';
    }
    public function getStatusNameAttribute()
    {
        return 1 == $this->getAttribute('status') ? "启用" : "禁用" ;
    }
}
